<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>切换焦点格式化</title>
<style type="text/css">
input[type="text"], select {
	width: 100px;
}

input:focus, select:focus {
	background-color: #00ff00;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" crossorigin="anonymous"></script>
</head>
<body>

	<input id="text1" type="text" value="2017/07/13" />

	<script type="text/javascript">
		$(function() {
			$('#text1').focus(function() {
				this.value = this.value.replace(/\/*/img, '');
			}).blur(function() {
				if (this.value.length == 8) {
					this.value = (/(\d{4})(\d{2})(\d{2})/img).exec(this.value).splice(1).join('/')
				}
			});
			;
		});
	</script>

</body>
</html>